package ui20131001.helper
{
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import controllers.managers.SingletonManager;
	import controllers.managers.SoundPlayerService;
	import controllers.managers.UIInstanceManager;
	
	import ui20131001.AnnouncementPanel;
	import ui20131001.DeviceInfoPanel;
	import ui20131001.DeviceListWindow;
	import ui20131001.FeedbackWin;
	import ui20131001.HistoryListWindow;
	import ui20131001.service.IndiAtOncePositionCmd;
	import ui20131001.service.IndiCurrPositionDataCmd;
	import ui20131001.service.IndiGetMemberDeviceCmd;
	
	import utils.UIDUtil;
	import utils.UIUtil;
	
	import view.option.DeviceInfoWindow;
	import view.option.NickNameWindow;

	/**
	 * 个人用户控制器,新的个人用户界面的控制器
	 * @author limin
	 * 
	 */
	public class IndividualUsersController
	{
		private static var _instance:IndividualUsersController;
		
		
		public var username:String = 'sbcs';
		public var password:String = 'sbcssbcs';
		
		public function IndividualUsersController()
		{
			if ( _instance )
			{
				throw new Error("不能直接实例化.");
			}
		}
		
		public static function getInst():IndividualUsersController
		{
			if ( _instance == null )
			{
				_instance = new IndividualUsersController();
			}
			return _instance;
		} 
		
		//----------
		/** 当前显示的设备数据 */
		[Bindable]
		public var currDevice:Object;
		
		/** 当前显示的位置数据 */
		private var _currPosition:Object;
		
		
		/** 主面板 */
		private function get maper():Maper
		{
//			return Maper(UIInstanceManager.getUI(Maper.NAME));
			return UIInstanceManager.getUI(Maper.NAME) as Maper
		}
		/** 设备列表 */
		private function get deviceListWindow():DeviceListWindow
		{
			return UIInstanceManager.getUI(DeviceListWindow.NAME) as DeviceListWindow
		}
		/** 设备信息 */
		private function get deviceInfoPanel():DeviceInfoPanel
		{
			return UIInstanceManager.getUI(DeviceInfoPanel.NAME) as DeviceInfoPanel
		}
		/** 历史轨迹 */
		private function get historyListWindow():HistoryListWindow
		{
			return UIInstanceManager.getUI(HistoryListWindow.NAME) as HistoryListWindow
		}
		/** 公告 */
		private function get announcementPanel():AnnouncementPanel
		{
			return UIInstanceManager.getUI(AnnouncementPanel.NAME) as AnnouncementPanel
		}
		/** 反馈窗口 */
		private function get feedbackWin():FeedbackWin
		{
			return UIInstanceManager.getUI(FeedbackWin.NAME) as FeedbackWin
		}
		
		/** 获取设备列表 */
		public function getMemberDevice():void
		{
			Global.openLoading();
			
			var cmd:IndiGetMemberDeviceCmd = new IndiGetMemberDeviceCmd(getMemberDeviceCallback);
			cmd.execute();
		}
		
		private function getMemberDeviceCallback(data:Object):void
		{
			Global.hideLoading();
			
			if(data.status == 1)
			{
				Global.deviceAllArray = new ArrayCollection(UIDUtil.update(data.data));
			}else
			{
				Global.deviceAllArray = new ArrayCollection();
			}
			
			if(Global.deviceAllArray.length==1)
			{
				IndividualUsersController.getInst().setCurrDevice(Global.deviceAllArray[0]);
			}else
			{
				openDeviceListWindow();
			}
		}
		
		/** 打开设备列表窗口 */
		public function openDeviceListWindow():void
		{
			deviceListWindow.update();
			deviceListWindow.visible = true;
			PopUpManager.addPopUp(deviceListWindow, maper);
			PopUpManager.centerPopUp(deviceListWindow);
//			UIUtil.setToTop(deviceListWindow, maper);
//			UIUtil.center(deviceListWindow, maper);
		}
		
		/** 打开历史轨迹窗口 */
		public function openHistoryListWindow():void
		{
			historyListWindow.update();
			historyListWindow.visible = true;
//			UIUtil.setToTop(historyListWindow, maper);
//			UIUtil.center(historyListWindow, maper);
//			historyListWindow.y = 150;
			historyListWindow.getTodayData();
			PopUpManager.addPopUp(historyListWindow, maper);
			PopUpManager.centerPopUp(historyListWindow);
		}
		
		/** 打开反馈窗口 */
		public function openFeedbacktWindow():void
		{
			feedbackWin.visible = true;
			PopUpManager.addPopUp(feedbackWin, maper);
			PopUpManager.centerPopUp(feedbackWin);
		}
		
		/** 打开昵称设置窗口 */
		public function openNicknameWindow():void
		{
			var nicknameWin:NickNameWindow = new NickNameWindow();
			nicknameWin.currDevice = currDevice;
			PopUpManager.addPopUp(nicknameWin, maper, true);
			PopUpManager.centerPopUp(nicknameWin);
		}
		
		/** 打开设备详情窗口 */
		public function openDeviceInfoWindow():void
		{
			var deviceInfoWin:DeviceInfoWindow = new DeviceInfoWindow();
			deviceInfoWin.currDevice = currDevice;
			PopUpManager.addPopUp(deviceInfoWin,maper,true);
			PopUpManager.centerPopUp(deviceInfoWin);
		}
		
		/** 设置当前显示的设备 */
		public function setCurrDevice(device:Object):void
		{
			Global.currentDeviceInfo = device;
			currDevice = device;
			if(currDevice)
			{
				showIndividualUsersUI(true);
				deviceInfoPanel.updateAccount(currDevice);
			}else
			{
				showIndividualUsersUI(false);
			}
		}
		
		/** 设置当前显示的坐标 */
		public function set currPosition(positionData:Object):void
		{
			_currPosition = positionData;
			deviceInfoPanel.updatePosition(_currPosition);
		}
		[Bindable]
		public function get currPosition():Object
		{
			return _currPosition;
		}
		
		/** 显示/隐藏个人面板 */
		private function showIndividualUsersUI(isShow:Boolean):void
		{
			UISwitch.show_announcementPanel = isShow;
			UISwitch.show_deviceInfoPanel = isShow;
			UISwitch.show_deviceMenuPanel = isShow;
			UISwitch.show_menuPanel = isShow;
			
//			maper.setZoomControlPosititon(100);
			setControlPosition(100);
		}
		
		public function setControlPosition(height:int=-1, width:int=-1):void
		{
			maper.mapCanvas.setControlsPosition(height, width);
		}
		
		/** 开始实时定位 */
		public function startGetAtOncePosition(callback:Function):void
		{
			var cPosCmd:IndiCurrPositionDataCmd = new IndiCurrPositionDataCmd(callback, currDevice.device_id);
			var cmd:IndiAtOncePositionCmd = new IndiAtOncePositionCmd(cPosCmd);
			cmd.execute();
		}
		
		/** 停止实时定位 */
		public function stopGetAtOncePosition():void
		{
			IndiAtOncePositionCmd.stop();
		}
		
		/** 开关声音 */
		public function switchSound():void
		{
			var sendPlayer:SoundPlayerService = SoundPlayerService(SingletonManager.getServ(SoundPlayerService.NAME));
			if(sendPlayer.getState())
			{
				sendPlayer.stop();
			}else
			{
				sendPlayer.play();
			}
		}
		
		public function updateAnnText():void
		{
			if(announcementPanel)
			{
				announcementPanel.updateText();
			}
		}
		
	}
}